package com.mlick.dddk.service;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

import com.mlick.dddk.DKApplication;
import com.mlick.dddk.R;
import com.mlick.dddk.utils.SysUtils;

import it.sauronsoftware.cron4j.Scheduler;

/**
 * Created by mlick on 2018-03-22.
 */
public class DKService extends Service {

    //AlarmManager aManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        SysUtils.out("===>onCreate");
        //aManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
        //scheduler = new Scheduler();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SysUtils.out("===>onStartCommand");
        Scheduler scheduler = DKApplication.getInstance().getScheduler();

        if (!scheduler.isStarted()) {
            //new Thread(new Runnable() {
            //    @Override
            //    public void run() {
            SysUtils.out("===>开始任务");
            //这种模式导致在星期一，星期二，星期三，星期四和星期五上午11:59启动任务。
            scheduler.schedule("28 8 * * 1,2,3,4,5", new MyRunable(1));
            scheduler.schedule("00 12 * * 1,2,3,4,5", new MyRunable(2));
            scheduler.schedule("50 12 * * 1,2,3,4,5", new MyRunable(3));
            scheduler.schedule("30 17 * * 1,2,3,4,5", new MyRunable(4));

            //这种模式会导致任务每分钟启动一次。
            scheduler.schedule("* * * * *", new MyRunable(-1));

            //此模式会导致每1分钟启动一次任务（0:00，0:05，0:10，0:15等）。
            //scheduler.schedule("*/5 * * * *", new MyRunable(1));

            //scheduler.schedule("9 18 * * 1,2,3,4,5", new MyRunable(4));
            //scheduler.schedule("10 18 * * 1,2,3,4,5", new MyRunable(4));
            //scheduler.schedule("11 18 * * 1,2,3,4,5", new MyRunable(4));
            //scheduler.schedule("30 18 * * 1,2,3,4,5", new MyRunable(4));

            scheduler.start();
            //    }
            //}).start();
        } else {
            SysUtils.out("===>任务已经开始了");
        }
        showNotificationProgress(this);
        return START_STICKY;
    }

    /**
     * 显示一个下载带进度条的通知
     *
     * @param context 上下文
     */
    public void showNotificationProgress(Context context) {
        /**进度条通知构建**/
        NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context, "2333366");
        /**设置为一个正在进行的通知**/
        builderProgress.setOngoing(true);
        /**设置小图标**/
        builderProgress.setSmallIcon(R.mipmap.ic_launcher);

        builderProgress.setContentTitle("DDDK提示(守护进程1)");
        builderProgress.setContentText("后台运行中....");

        /**新建通知自定义布局**/
        //mRemoteViews = new RemoteViews(context.getPackageName(), R.layout);
        ///**进度条ProgressBar**/
        //mRemoteViews.setProgressBar(R.id.pb, 100, 0, false);
        ///**提示信息的TextView**/
        //mRemoteViews.setTextViewText(R.id.tv_message, "下载中...");
        ///**操作按钮的Button**/
        //mRemoteViews.setTextViewText(R.id.bt, "暂停");
        ///**设置左侧小图标*/
        //mRemoteViews.setImageViewResource(R.id.iv, R.mipmap.ic_launcher);
        ///**设置通过广播形式的PendingIntent**/
        //Intent intent = new Intent(BROADCAST_ACTION_CLICK);
        //PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE_BROADCAST, intent, 0);
        //mRemoteViews.setOnClickPendingIntent(R.id.bt, pendingIntent);
        ///**设置自定义布局**/
        //builderProgress.setContent(mRemoteViews);
        ///**设置滚动提示**/
        //builderProgress.setTicker("开始下载...");


        Notification notification = builderProgress.build();
        /**设置不可手动清除**/
        notification.flags = Notification.FLAG_NO_CLEAR;

        /**获取通知管理器**/
        //notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        ///**发送一个通知**/
        //notificationManager.notify(NOTIFICATION_ID, notification);

        startForeground(123456, notification);
    }


    @Override
    public void onDestroy() {
        SysUtils.out("===>结束任务");
        DKApplication.getInstance().stop();
        stopForeground(true);
        //Intent intent = new Intent("com.mlick.dddk.service.destory");
        //sendBroadcast(intent);
        //startService(new Intent(this, DKService.class));
        super.onDestroy();
    }

    @Override
    public void onTrimMemory(int level) {
        startService(new Intent(this, DKServiceDeamon.class));
    }


}
